// pages/goods_detail/goods_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopType: "addShopCar", //默认购买类型为加入购物车
    isShow: false, //默认隐藏
    buyNumber: 1,
    shopCarInfo: [],  //购物车信息
    shopNum: 0    //购物车数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goodsid = options.goodsid;  //获取点击物品的goodsid
    var that = this; //对象已经发生改变，需要用that来代替
    wx.request({
      url: 'http://111.230.44.126:88/api/v1/product/' + goodsid,
      header: {
        "content-type": "application/json",
        "token": wx.getStorageSync("token")
      },
      success: function (res) {
        that.setData({
          name: res.data.name,
          main_img_url: res.data.main_img_url,
          price: res.data.price,
          stock: res.data.stock,
          id: res.data.id
        })
      }
    })

    /*/获取购物车缓存的数据
    var that=this;
    wx.getStorage({
      key: 'shopCar',
      success: function(res) {
         that.setData({
             shopCarInfo:res.data,
             shopNum:res.data.shopNum
          })
      },
    }) */

  },
  //获取缓存中购物车的数量
  onShow: function () {
    var arr = wx.getStorageSync("shopCar") || [];
    var Number=0;
    if (arr.length > 0) {
      for (var i in arr) {
        //数量进行合计
        Number = Number+ arr[i].buyNumber;
      }
      this.setData({
        shopNum: Number
      })
      try {
        wx.setStorageSync("shopCar", arr)
      }
      catch (e) {
        console.log(e);
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //购物车
  goShopCar: function () {
    wx.switchTab({
      url: '../Shopping_Cart/Shopping_Cart',
    })
  },
  //加入购物车打开购物详情界面
  toAddShopCar: function () {
    this.setData({
      shopType: "addShopCar",
      isShow: true
    })
  },
  //立即购买打开购物详情界面
  tobuy: function () {
    this.setData({
      shopType: "tobuy",
      isShow: true
    })
  },
  //关闭购买界面
  isClose: function () {
    this.setData({
      isShow: false
    })
  },
  //购买数量控制
  numJianTap: function (e) {
    if (this.data.buyNumber <= 1) {
      buyNumber: 1
    } else {
      this.setData({
        buyNumber: this.data.buyNumber - 1
      })
    }
  },
  numJiaTap: function (e) {
    this.setData({
      buyNumber: this.data.buyNumber + 1
    })
  },
  addShopCar: function (e) {
    this.isClose(); //将购买详情的规格框隐藏
    wx.showToast({
      title: "成功加入购物车"  //弹出提示框
    })
    //遍历商品列表
    this.data.shopNum = this.data.buyNumber + this.data.shopNum;
    var arr = wx.getStorageSync("shopCar") || [];
    arr.shopNum = this.data.shopNum;
    this.setData({
      shopNum:this.data.shopNum
    })
    //判断购物车中是否有数据
    if (arr.length > 0) {
      //遍历购物车数据
      for (var j in arr) {
        if (arr[j].id == this.data.id) {
          arr[j].buyNumber = arr[j].buyNumber+this.data.buyNumber;
          arr[j].shopNum = this.data.shopNum;
          try {
            wx.setStorageSync("shopCar", arr)
            console.log(arr);
          }
          catch (e) {
            console.log(e);
          }
          return;
        }
      }
      //遍历完购物车后
      arr.push(this.data);//将当前数据放在购物车组中
    }
    else {    
      arr.push(this.data);//将当前数据放在购物车组中
    }
    
    //把购物车数据存放在缓存中
    try {
      wx.setStorageSync("shopCar", arr)
    }
    catch (e) {
      console.log(e)
    }
  }

})